﻿#define _CRT_SECURE_NO_WARNINGS
//strstr库函数，用一个字符串找另一个字符串
//string string
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main()
{
	char arr1[] = "abbbbcdefabcdef";
	char arr2[] = "bbc";
	const char* ret = strstr(arr1, arr2);
	if (ret == NULL)
		printf("no\n");
	else
		printf("%s\n",ret);
	return 0;
}

//模拟实现strstr函数
#include <stdio.h>
#include <assert.h>
const char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	//定义两个指针来进行匹配
	const char* s1 = str1;
	const char* s2 = str2;

	//cur指针用来标记可能进行匹配的位置
	const char* cur = str1;
    //若cur未指向'\0'
	while (*cur)
	{
		//进行一次匹配
		s1 = cur;
		s2 = str2;
		while(*s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return cur;
		//cur每次匹配完后++
		cur++;
	}
	return NULL;
}
int main()
{
	char arr1[] = "abbbbcdefabcdef";
	char arr2[] = "bbc";
	const char* ret = my_strstr(arr1, arr2);
	if (ret == NULL)
		printf("no\n");
	else
		printf("%s\n",ret);
	return 0;
}

//strstr函数和strncpy函数、puts函数
#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "This is a simple string";
    char* pch;
    pch = strstr(str, "simple");
    if (pch != NULL)
        strncpy(pch, "abcdef", 6);
    puts(str);
    return 0;
}

//strtok函数
//char * strtok ( char * str, const char * sep);
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "256@178#886.772";
	char arr2[] = "@#.";

	char* ps = strtok(arr1, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	return 0;
}

//相同的分割符
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "256.178.886.772";
	char arr2[] = ".";

	char* ps = strtok(arr1, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	return 0;
}

//分割符的顺序
#include <stdio.h>
#include <string.h>
int main()
{
		char arr1[] = "256@178#886.772";
		char arr2[] = ".#@";

	char* ps = strtok(arr1, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	ps = strtok(NULL, arr2);
	printf("%s\n", ps);
	return 0;
}

//优化代码
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "256@178#886.772";
	char arr2[] = ".#@";

	for (char* ps = strtok(arr1, arr2); ps != NULL; ps = strtok(NULL, arr2))
	{
		printf("%s\n", ps);
	}
	return 0;
}

//strerror函数的使⽤
//string error --- 字符串报错
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%s\n", strerror(i));
	}
	return 0;
}

//strerror函数举例
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
	FILE* pFile;
	pFile = fopen("test1.txt", "r");
	if (pFile == NULL)
		printf("打开错误:%s\n", strerror(errno));
	return 0;
}

//perror函数
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
	FILE* pFile;
	pFile = fopen("test1.txt", "r");
	if (pFile == NULL)
	{
		perror("文件打开失败，原因是");
		return 1;
	}
	fclose(pFile);
	pFile = NULL;
	return 0;
}